import {
	Catch,
	ExceptionFilter,
	ArgumentsHost,
	HttpStatus,
	HttpException
} from '@nestjs/common'

@Catch()
export class AllExceptionFilter implements ExceptionFilter {
	catch(exception: unknown, host: ArgumentsHost) {
		const ctx = host.switchToHttp()
		const request = ctx.getRequest()
		const response = ctx.getResponse()
		// 处理所有的意外
		const status = exception instanceof HttpException ?
			exception.getStatus : HttpStatus.INTERNAL_SERVER_ERROR

		response.status(status)
			.json({
				statusCode: status,
				timestamp: new Date().toISOString(),
				path: request.url
			})
	}
}

